<?php

/*base calss for import.*/

class ImportBase
{
	protected $s_db; //soucrce database.
	private $db_host;
	private $db_user;
	private $db_pass;
	private $db_character;
	
	protected $db;
	
	protected $log_table;
	
	private $api; //api object
	
	protected $import_counter;
	
	protected $import_num = 100;
	
	//protected $d_db; //destination database.
	
	public function __construct()
	{
		$this->log_table = "temp_log_for_import";
		$this->create_log_table();
		$this->import_counter = 0;
		
		$this->db  = Mage::getSingleton('core/resource')->getConnection('core_write');
	}
	
	public function set_s_db( $host,$db_user,$db_pass,$db_name,$character='utf8' )
	{
		include_once("myMysqlDB.class.php");
		$this->s_db = new myMysqlDB( $host,$db_user,$db_pass,$db_name,$character );
	}
	
	
	public function create_log_table()
	{
		$query = '';
		//$query .= "DROP TABLE IF EXISTS `{$this->log_table}`;";
		$query .= "CREATE TABLE IF NOT EXISTS `{$this->log_table}` (
				  `log_id` int(11) NOT NULL auto_increment,
				  `org_category_id` int(11),
				  `category_id` int(11),
				  `org_product_id` int(11),
				  `product_id` int(11),
				  `sku` varchar(255),
				  `images_import` tinyint,
				  `org_customer_id` int(11),
				  `customer_id` int(11),
				  `sku_update` tinyint,
				  `tax_class` tinyint,
				  PRIMARY KEY  (`log_id`),
				  KEY `org_category_id` (`org_category_id`),
				  KEY `category_id` (`category_id`),
				  KEY `org_product_id` (`org_product_id`),
				  KEY `product_id` (`product_id`),
				  KEY `org_customer_id` (`org_customer_id`),
				  KEY `customer_id` (`customer_id`),
				  KEY `images_import` (`images_import`)
				) ENGINE=InnoDb  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;";
		
		$this->s_db->query( $query );		
	}
	
	protected function close_log_table()
	{
		
	}
	
	public function run()
	{
		
	}
	
	public function log( $data )
	{
		$query = " select count(*) as total_rows from `{$this->log_table}` where ";
		foreach( $data as $key => $var ){
			$query .= " {$key} = '{$var}' and ";
		}
		
		$query = trim( $query,'and ' );
		$this->s_db->query( $query );		
		$this->s_db->next_record();
		if( !$this->s_db->f('total_rows') ){	
			$query = " insert into `{$this->log_table}` ";	
			$fields = '';
			$values = '';
			foreach( $data as $key => $var ){
				$fields .= "`{$key}`,";
				$values .= "'{$var}',";
			}
			
			$fields = trim( $fields,',' );
			$values = trim( $values,',' );
			
			$query .= "({$fields}) values($values)";
			$this->s_db->query( $query );	
		}	
	}
	
	public function read_log( $data )
	{
		$query = " select *  from `{$this->log_table}` where ";
		foreach( $data as $key => $var ){
			$query .= " {$key} = '{$var}' and ";
		}
		
		$query = trim( $query,'and ' );
		$this->s_db->query( $query );	
		return $this->s_db->af();
	}
		
	public function allStoreViews()
	{
		static $allStores;
		if( !empty( $allStores ) ){
			return $allStores;
		}else{
			$allStores = $this->db->fetchAll ( "select store_id,name,code from " . 
	                    Mage::getSingleton ( 'core/resource' )->getTableName ( "core_store" ) . 
	                    " where store_id>0" );
	        return $allStores;
		}
	}
	
	public function getStoreIdByCode( $code )
	{
		static $store_ids;
		if( isset( $store_ids[ $code ] ) ){
			return $store_ids[ $code ];
		}else{
			$storeviews = $this->allStoreViews();
			foreach( $storeviews as $views ){
				if( $views['code'] == $code ){
					$store_ids[ $code ] = $views['store_id'];
				}
			}
			
			return $store_ids[ $code ];
		}
	}
	
	protected function transation_commit()
	{
		$this->import_counter++;		
		if( $this->import_counter > $this->import_num ){
			$this->redirect( Mage::getBaseUrl( 'web' ) . "scripts_import/import.php?time=" . time() );
			exit();
		}
	}
	
	protected function redirect( $url )
	{
		echo "<script type=\"text/javascript\">\n";
		echo "document.location=\"".$url."\";";
		echo "</script>\n";
	}
}